function handleLoginError(error) {
  var result;
  if (error.name == "PermanentError" || error.name == "TemporaryError") {
    result = "Incorrect username or password";
  }
  else if (error.name == "ConnectError" || error.name == "CreateSocketError") {
    // display "Could not connect to host, error code?"
    result = "Could not connect to host: " + error.message;
  }
  else if (error.name == "FTPViolationError" || error.name == "UnexpectedReplyError") {
    // display closing connection, server does not adhere to FTP protocol
    result = "Server not adhering to FTP protocol. Closing connection.";
  }
  else {
    // unexpected problem occured, please try again
    console.log(error);
    result = "Unexpected error occured. Please try again.";
  }
  document.getElementById("message").style.color = "red";
  $('#message').text(result);
}

function displayNewWindow (client, listDir){
  var onOpenWindowComplete = function (createdWindow) {
    console.log("created new window");
    createdWindow.contentWindow.directoryList = listDir;
    createdWindow.contentWindow.session = client;
    console.log("Was listRemote called?", clients);
  };
  chrome.app.window.create('twoPane.html', {
  'bounds': {
    'width': 1130,
    'height': 665
  },
  minWidth: 1130,
  minHeight: 665,
  maxWidth: 1130,
  maxHeight: 665
  }, onOpenWindowComplete);
}

$(document).ready(function() {
  $('#message').text("Please enter your credentials");
	$("#connect").click(function() {
    var host = $("#host").val();
    var username = $("#user").val();
    var password = $("#password").val();
    var port = $("#port").val();
    var client = new FTPSession(host, port);
		client.login(username, password).then(function (welcomeMsg) {
      document.getElementById("message").style.color = "green";
      $('#message').text("Connected to host " + host);
      client.listDir().then(function (listDir) {
        console.log("got listDirectory, now calling display new window", clients)
        displayNewWindow(client, listDir);
      }).catch(function (error) {
        // listDir error handling
        console.log("LIST DIR ERRORRRRRRRRRRRRRR", error);
      });
		}).catch(function (error) {
      console.log("Caught errrol");
      handleLoginError(error);
    });
	});
});










